using System;
using System.Data; 
using System.Collections; 
using System.Collections .Generic;
//using CCS.Engineering.StructuralElements;
using BuildingStructuralElements.SectionsEC4;

namespace BuildingStructuralElements
{
	
    
	public class SectionManagerEC4 : ISectionManager
	{
		public DBAccess db;

        private static SectionManagerEC4 instance;
        public static SectionManagerEC4 Inscatnce
        {
            get
            {
                if (instance == null)
                    instance = new SectionManagerEC4();
                return instance;
            }
        }

		public SectionManagerEC4()
		{
			db = new DBAccess();
            instance = this;
            			
		}

		public SectionManagerEC4(string path)
		{
			db = new DBAccess(path);
            instance = this;
		}

        internal void LoadSectionFile(string path)
        {
            db = new DBAccess(path);
        }

		public DataTable GetSectionsTable(string type)
		{
			if (db.SectionTableExists(type))
				return db.get_SectionTable(type);		
			else
				return null;
		}

		public int GetSectionIndex(DataTable table ,string type,string name)
		{
			if (db.SectionExists(type,name))
				return db.get_sectionIndex(table,type,name);		
			else
				return -1;
			
		}

        public bool ExistSection(string type, string name)
        {
            return db.SectionExists(type, name);
        }

		public Section GetSection(string type, string name)
		{
            return GetSectionRefactored(type, name);
			if (!db.SectionExists(type,name)) return null ;

			string sectionName = type + " " + name;

			#region HEA && IPE
			if (type == "HEA" || type == "IPE")
			{
				DataTable table  = db.get_SectionTable(type);   
				int index1 = db.get_sectionIndex(table,type,name);
				
				float h;  
				float b;  
				float t; 
				float s; 
				float r;   

				if (type == "HEA")
				{
					SteelDataSet.Sections_HEARow  row = (SteelDataSet.Sections_HEARow)table.Rows[index1];  
					h = (float)row.h; 
					b = (float)row.b; 
					t = (float)row.tf;
					s = (float)row.tw; 
					r = (float)row.r; 
				}
				else
				{
					SteelDataSet.Sections_IPERow  row = (SteelDataSet.Sections_IPERow)table.Rows[index1];  
					h = (float)row.h; 
					b = (float)row.b; 
					t = (float)row.tf;
					s = (float)row.tw; 
					r = (float)row.r; 
				}
             
                Section Section = new SectionIRolled(sectionName ,h,b,t,s,r);    

                return Section;
                /*
				if (beam is VariableBeam)
				{
                    VariableBeam b1 = (VariableBeam)beam;
 				    b1.BeamEndSection = new SectionIWelded();    
					b1.Section = new SectionIWelded();    
				}
                 * */
				
			}
			#endregion

			#region SHS
			if (type == "SHS")
			{
				DataTable table  = db.get_SectionTable(type);   
				int index1 = db.get_sectionIndex(table,type,name);
				
				SteelDataSet.Sections_SHSRow row = (SteelDataSet.Sections_SHSRow)table.Rows[index1];  
				                
				float h = (float)row.b; 
				float t = (float)row.t;
				float re = 3; 
				float ri = 3; 
              				
				Section Section = new SectionSHS(sectionName ,h,t,re,ri);

                return Section;		
							
			}
			#endregion

			#region RHS
			if (type == "RHS")
			{
				DataTable table  = db.get_SectionTable(type);   
				int index1 = db.get_sectionIndex(table,type,name);
				
				SteelDataSet.Sections_RHSRow row = (SteelDataSet.Sections_RHSRow)table.Rows[index1];  
				                
				float h = (float)row.h; 
				float b = (float)row.b; 
				float t = (float)row.t;
				float re = 3; 
				float ri = 3; 
              				
				 Section Section = new SectionRHS(sectionName,h,b,t,re,ri);

                 return Section;
 			
							
			}
			#endregion

			#region CHS
			if (type == "CHS")
			{
				DataTable table  = db.get_SectionTable(type);   
				int index1 = db.get_sectionIndex(table,type,name);
				
				SteelDataSet.Sections_CHSRow  row = (SteelDataSet.Sections_CHSRow)table.Rows[index1];  

                float d = (float)row.d; 
				float t = (float)row.t; 
              			
				Section Section =new SectionCHS(sectionName,d,t);

                return Section;	
			
			}

            return null;


			#endregion
		
		}

        public Section GetSectionRefactored(string type, string name)
        {
            if (!db.SectionExists(type, name)) return null;

            string sectionName = type + " " + name;

            #region HEA && IPE
            if (type == "HEA" || type == "IPE")
            {
                DataTable table = db.get_SectionTable(type);
                int index1 = db.get_sectionIndex(table, type, name);

                float h;
                float b;
                float t;
                float s;
                float r;

                if (type == "HEA")
                {

                    h = (float)GetSectionInertiaAttribute(type, name, "h");
                    b = (float)GetSectionInertiaAttribute(type, name, "b"); //row.b;
                    t = (float)GetSectionInertiaAttribute(type, name, "tf"); //row.tf;
                    s = (float)GetSectionInertiaAttribute(type, name, "tw"); //row.tw;
                    r = (float)GetSectionInertiaAttribute(type, name, "r"); //row.r;
                }
                else
                {
                    h = (float)GetSectionInertiaAttribute(type, name, "h");
                    b = (float)GetSectionInertiaAttribute(type, name, "b");// row.b;
                    t = (float)GetSectionInertiaAttribute(type, name, "tf");// row.tf;
                    s = (float)GetSectionInertiaAttribute(type, name, "tw");// row.tw;
                    r = (float)GetSectionInertiaAttribute(type, name, "h"); //row.r;
                }

                Section Section = new SectionIRolled(sectionName, h, b, t, s, r);

                return Section;
                /*
				if (beam is VariableBeam)
				{
                    VariableBeam b1 = (VariableBeam)beam;
 				    b1.BeamEndSection = new SectionIWelded();    
					b1.Section = new SectionIWelded();    
				}
                 * */

            }
            #endregion

            #region SHS
            if (type == "SHS")
            {
                DataTable table = db.get_SectionTable(type);
                int index1 = db.get_sectionIndex(table, type, name);

                SteelDataSet.Sections_SHSRow row = (SteelDataSet.Sections_SHSRow)table.Rows[index1];

                float h = (float)row.b;
                float t = (float)row.t;
                float re = 3;
                float ri = 3;

                Section Section = new SectionSHS(sectionName, h, t, re, ri);

                return Section;

            }
            #endregion

            #region RHS
            if (type == "RHS")
            {
                DataTable table = db.get_SectionTable(type);
                int index1 = db.get_sectionIndex(table, type, name);

                SteelDataSet.Sections_RHSRow row = (SteelDataSet.Sections_RHSRow)table.Rows[index1];

                float h = (float)row.h;
                float b = (float)row.b;
                float t = (float)row.t;
                float re = 3;
                float ri = 3;

                Section Section = new SectionRHS(sectionName, h, b, t, re, ri);

                return Section;


            }
            #endregion

            #region CHS
            if (type == "CHS")
            {
                DataTable table = db.get_SectionTable(type);
                int index1 = db.get_sectionIndex(table, type, name);

                SteelDataSet.Sections_CHSRow row = (SteelDataSet.Sections_CHSRow)table.Rows[index1];

                float d = (float)row.d;
                float t = (float)row.t;

                Section Section = new SectionCHS(sectionName, d, t);

                return Section;

            }

            return null;


            #endregion

        }

        public double GetSectionInertiaAttribute(string type ,string name,string attribute)
        {
            DataTable table = GetSectionsTable(type);
            if (table == null) throw new Exception("There is no such section type in DataBase");

            if (!table.Columns.Contains(attribute)) throw new Exception("Section do not contain" + attribute);
            DataRow[] rows = table.Select("name='" + name + "'");
            if (rows != null)
                return (double)rows[0][attribute];

            throw new Exception("There is no Section " + name + " of type " + table.TableName);
        }

        private IList<System.Data.DataTable> GetTables()
        {
            return db.GetDataTables();
        }

        public List<string> GetListOfSectionsNames(string type)
        {
            List<string> sections = new List<string>();            

            return sections;
        }
        public List<string> GetListOfSectionsNames()
        {
            List<string> sections = new List<string>();

            return sections;
        }
    }
}
